/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.openbravo.bean.sales;

import e.odbo.data.bean.I_AutoGeneratorStringKey;
import e.odbo.data.bean.I_ModifiedLogBean;
import com.openbravo.bean.Product;
import com.openbravo.bean.Serving;
import com.openbravo.bean.Tax;
import com.openbravo.bean.shard.I_CalcLine;
import com.openbravo.def.I_TaxCalcLine;
import java.util.Date;
import java.util.HashMap;

/**
 * 
 * @author Administrator
 */
public class TicketLine implements I_ModifiedLogBean<String>,
		I_TaxCalcLine, I_CalcLine ,I_AutoGeneratorStringKey{
	private static final long serialVersionUID = 6608012948284450199L;
	public String id;
	public String m_sTicket;
	public int m_iLine;
	public double multiply;
	public double price;// 实售价
	public Tax tax;
	public HashMap<String, String> attributes;
	public String productid;
	public String attsetinstid;
    private String lotNo;
    private Date validityDate;

	public double defaultPrice;// 此行商品的原价（可以是普通价或会员价），是进行二次计算的基础价
	public double priceRate;// 单行折扣率(如果为整单折扣的，此值为1，否则为单行折扣
	public boolean isPrefSell;// 用于断定当前销售的商品是否是特价商品

	public static String TICKETlINE_EXTPROP = "extprop";
	public Serving servings;
	public double consultCost;// 参考成本
	private Date lastModified;
	
	public double score;
	public String saleMan;//销售员
	
	private double attrPrice;// 属性差价
	private double attrPriceCost;//成本
	

	/** Creates new TicketLine */
	public TicketLine(String productid, double dMultiply, double dPrice,
			Tax tax, HashMap props, double rate) {
		init(productid, null, dMultiply, dPrice, tax, props, rate, 0);
	}

	public TicketLine(String productid, double dMultiply, double dPrice,
			Tax tax, double rate) {
		init(productid, null, dMultiply, dPrice, tax, new HashMap(), rate, 0);
	}

	public TicketLine(String productid, String productname,
			String producttaxcategory, double dMultiply, double dPrice,
			Tax tax, double rate) {
		HashMap props = new HashMap();
		props.put("product.name", productname);
		props.put("product.taxcategoryid", producttaxcategory);
		init(productid, null, dMultiply, dPrice, tax, props, rate, 0);
	}

	public TicketLine(String productname, String producttaxcategory,
			double dMultiply, double dPrice, Tax tax, double rate) {

		HashMap props = new HashMap();
		props.put("product.name", productname);     
		props.put("product.taxcategoryid", producttaxcategory);
		init(null, null, dMultiply, dPrice, tax, props, rate, 0);
	}

	public TicketLine() {
		init(null, null, 0.0, 0.0, null, new HashMap(), 1, 0);
	}

	public TicketLine(Product product, double dMultiply, double dPrice,
			Tax tax, HashMap attributes, double rate) {

		String pid;

		if (product == null) {
			pid = null;
		} else {
			pid = product.getID();
			attributes.put("product.name", product.getName());
			attributes.put("product.com", product.isCom() ? "true" : "false");
			if (product.getAttributeSetID() != null) {
				attributes.put("product.attsetid", product.getAttributeSetID());
			}
			attributes.put("product.taxcategoryid", product.getTaxCategoryID());
			if (product.getCategoryID() != null) {
				attributes.put("product.categoryid", product.getCategoryID());
			}
			attributes.put("product.manufacturing", product.isM_bManufacturing() ? "true" : "false");
			attributes.put("product.isBomStore", product.isBomStore()? "true" : "false");
		}
		init(pid, null, dMultiply, dPrice, tax, attributes, rate,
				product.getPriceBuy());
	}

	public TicketLine(Product oProduct, double dPrice, Tax tax,
			HashMap attributes, double rate) {
		this(oProduct, 1.0, dPrice, tax, attributes, rate);
	}

	public TicketLine(Serving servings, Tax tax, double rate) {
		init(null, null, 1, 0.0, tax, new HashMap(), rate, 0);
		setServings(servings);
	}

	protected void init(String productid, String attsetinstid,
			double dMultiply, double dPrice, Tax tax, HashMap attributes,
			double rate, double cost) {
		this.productid = productid;
		this.attsetinstid = attsetinstid;
		multiply = dMultiply;
		price = dPrice;
		this.tax = tax;
		this.attributes = attributes;

		defaultPrice = price;
		priceRate = rate;
		isPrefSell = false;

		m_sTicket = null;
		m_iLine = -1;
		consultCost = cost;
		
		score=0;
		attrPrice=0;
		attrPriceCost=0;
	}

	void setTicket(String ticket, int line) {
		m_sTicket = ticket;
		m_iLine = line;
	}

	public TicketLine copyTicketLine() {
		TicketLine l = new TicketLine();
		// l.m_sTicket = null;
		// l.m_iLine = -1;
		l.productid = productid;
		l.attsetinstid = attsetinstid;
		l.multiply = multiply;
		l.price = price;
		l.tax = tax;
		l.attributes = (HashMap) attributes.clone();

		l.isPrefSell = isPrefSell;
		l.defaultPrice = defaultPrice;
		l.priceRate = priceRate;
		l.servings = servings;
		l.consultCost = consultCost;
		l.score=score;
		l.saleMan=saleMan;
		l.attrPrice=attrPrice;
		l.attrPriceCost=attrPriceCost;
		return l;
	}

	public Serving getServings() {
		return servings;
	}

	public void setServings(Serving servings) {
		this.servings = servings;
		setProperty("product.name", servings.getM_sName());
		setProperty(PROPERTY_ServingsId, servings.getM_sId());
	}

	 public boolean isBomStore(){
	    	return "true".equals(getProperty("product.isBomStore","true"));
	 }
	
	public boolean isIsPrefSell() {
		return isPrefSell;
	}

	public void setIsPrefSell(boolean isPrefSell) {
		this.isPrefSell = isPrefSell;
	}

	public void setPrefSellID(String prefId){
		setProperty("product.prefSellID", prefId);
	}
	
	public String getPrefSellID(){
		return getProperty("product.prefSellID");
	}
	
	public double getPriceRate() {
		return priceRate;
	}

	public void setPriceRate(double rate) {
		priceRate = rate;
	}

	public int getTicketLine() {
		return m_iLine;
	}

	public String getProductID() {
		return productid;
	}

	public String getProductName() {
		return attributes.get("product.name");
	}

	public String getProductAttSetId() {
		return attributes.get("product.attsetid");
	}

	public String getProductAttSetInstDesc() {
		return getProperty("product.attsetdesc", "");
	}

	public void setProductAttSetInstDesc(String value) {
		if (value == null) {
			attributes.remove(value);
		} else {
			attributes.put("product.attsetdesc", value);
		}
	}

	public String getProductAttSetInstId() {
		return attsetinstid;
	}

	public void setProductAttSetInstId(String value) {
		attsetinstid = value;
	}

	public boolean isProductCom() {
		return "true".equals(getProperty("product.com"));
	}
	
	public boolean isManufacturing(){
		return "true".equals(getProperty("product.manufacturing"));
	}

	public String getProductTaxCategoryID() {
		return (getProperty("product.taxcategoryid"));
	}
	
	public String getProductCategoryID() {
		return (getProperty("product.categoryid"));
	}

	public double getMultiply() {
		return multiply;
	}

	public void setMultiply(double dValue) {
		multiply = dValue;
	}

	public Tax getTaxInfo() {
		return tax;
	}

	public void setTaxInfo(Tax value) {
		tax = value;
	}

	public String getProperty(String key) {
		return attributes.get(key);
	}

	public String getProperty(String key, String defaultvalue) {
		String ret = attributes.get(key);
		return ret != null ? ret : defaultvalue;
	}

	public void setProperty(String key, String value) {
		attributes.put(key, value);
	}

	public HashMap getProperties() {
		return attributes;
	}

	public boolean isServiceTicketLine() {
		return attributes.containsKey(PROPERTY_ServingsId);
	}

	public double getTaxRate() {
		return tax == null ? 0.0 : tax.getRate();
	}

	public double getConsultCost() {
		return consultCost;
	}

	public void setConsultCost(double consultCost) {
		this.consultCost = consultCost;
	}

	@Override
	public Date getLastModified() {
		return lastModified;
	}

	@Override
	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	public String getM_sTicket() {
		return m_sTicket;
	}

	public void setM_sTicket(String m_sTicket) {
		this.m_sTicket = m_sTicket;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public String getProductid() {
		return productid;
	}

	public void setProductid(String productid) {
		this.productid = productid;
	}

	public double getDefaultPrice() {
		return defaultPrice;
	}

	public void setDefaultPrice(double defaultPrice) {
		this.defaultPrice = defaultPrice;
	}

	public boolean isPrefSell() {
		return isPrefSell;
	}

	public void setPrefSell(boolean isPrefSell) {
		this.isPrefSell = isPrefSell;
	}

	public boolean isServiceOrderLine() {
		return attributes.containsKey(PROPERTY_ServingsId);
	}

	public Date getStartData() {
		String startDataStr = getProperty(PROPERTY_ServingsStart);
		if (startDataStr != null)
			return new Date(Long.parseLong(startDataStr));
		return null;
	}

	public void setStartData(Date start) {
		attributes.put(PROPERTY_ServingsStart, String.valueOf(start.getTime()));
	}

	public void setEndData(Date end) {
		attributes.put(PROPERTY_ServingsEnd, String.valueOf(end.getTime()));
	}

	public Date getEndData() {
		String endDataStr = getProperty(PROPERTY_ServingsEnd);
		if (endDataStr != null)
			return new Date(Long.parseLong(endDataStr));
		return null;
	}

	public String getServiceId() {
		return getProperty(PROPERTY_ServingsId);
	}

	public String getRantCycleStyle() {
		return this.getProperty(PROPERTY_ServingsCyclesType);
	}

	public int getM_iLine() {
		return m_iLine;
	}

	public void setM_iLine(int m_iLine) {
		this.m_iLine = m_iLine;
	}

	public Tax getTax() {
		return tax;
	}

	public void setTax(Tax tax) {
		this.tax = tax;
	}

	public HashMap<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(HashMap<String, String> attributes) {
		this.attributes = attributes;
	}

	public String getAttsetinstid() {
		return attsetinstid;
	}

	public void setAttsetinstid(String attsetinstid) {
		this.attsetinstid = attsetinstid;
	}

	public String getLotNo() {
		return lotNo;
	}

	public void setLotNo(String lotNo) {
		this.lotNo = lotNo;
	}

	public Date getValidityDate() {
		return validityDate;
	}

	public void setValidityDate(Date validityDate) {
		this.validityDate = validityDate;
	}

	@Override
	public void setKey(String key) {
		this.id=key;
	}

	@Override
	public String getKey() {
		return id;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}
	
	public String getExtProp(){
		return getProperty(TICKETlINE_EXTPROP);
	}

	public String getSaleMan() {
		return saleMan;
	}

	public void setSaleMan(String saleMan) {
		this.saleMan = saleMan;
	}

	public double getAttrPrice() {
		return attrPrice;
	}

	public void setAttrPrice(double attrPrice) {
		this.attrPrice = attrPrice;
	}

	public double getAttrPriceCost() {
		return attrPriceCost;
	}

	public void setAttrPriceCost(double attrPriceCost) {
		this.attrPriceCost = attrPriceCost;
	}	
}
